/**
 * @file Config.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2020-09-10
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */
// #pragma once

#ifndef __GB_CONFIG_2_H__
#define __GB_CONFIG_2_H__

#include <string>
#include "STL_YAML/STL_YAML.h"
#include "yaml-cpp/yaml.h"

namespace GBXUEAN
{
    class Config
    {
    private:
        Config();
        ~Config() = default;

    public:
        static Config *instance();
        /**
         * @brief 从文件加载配置文件
         * 
         * @param path 
         * @return true 
         * @return false 
         */
        bool Load(const std::string &path);
        /**
         * @brief 显示配置文件信息
         * 
         */
        void showConfig();
        /**
         * @brief Get the Config As String 
         * 
         * @param path 
         * @return std::string 
         */
        std::string getConfigAsString(std::string path);
        /**
         * @brief Get the Config As Uint32 object
         * 
         * @param path 
         * @return uint32_t 
         */
        uint32_t getConfigAsUint32(std::string path);
        /**
         * @brief Get the Config As Int32 object
         * 
         * @param path 
         * @return int32_t 
         */
        int32_t getConfigAsInt32(std::string path);
        /**
         * @brief 获取配置数组的Size
         * 
         */
        size_t getConfigArraySize(std::string path);
        /**
         * @brief 获取配置信息
         * 
         * @tparam T 
         * @param path 
         * @param value 
         * @return true 
         * @return false 
         */
        template <typename T>
        bool getConfig(std::string path, T &value);
    private:
        YAML::Node m_node;
    };

    template <typename T>
    bool Config::getConfig(std::string path, T &value)
    {
        try
        {
            if (!STL::STL_YAML::getValue(m_node, value, path))
            {
                std::cout << "Config get Error:" << path << std::endl;
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what() << '\n';
            return false;
        }
    }

} // namespace GBXUEAN

#endif // __GB_CONFIG_2_H__